<template>
  <div class='hello'>
    <Invatation meetingname='2018年秋季商品培训反馈信息' listtitle='参加人员名单及联系方式' :invatation='invatation1' hidephototip />
    <Invatation meetingname='2019年春季订货会反馈信息' listtitle='参加人员名单' :invatation='invatation2' hidecityandcustomer />
    <Button type='primary' size='large' style='margin-left: 150px; margin-top: 50px;' @click='submit'>提交信息</Button>
  </div>
</template>

<script>
import Invatation from './Invatation'

export default {
  name: 'Submit',
  components: {
    Invatation
  },
  data() {
    return {
      invatation1: {
        meetingname: '2018年秋季商品培训反馈信息',
        cityname: '',
        customername: '',
        number: 1,
        list: '',
        arrivaltime: '2018-06-21',
        needbook: false,
        hotelinfo: {
          hotelname: '维也纳龙华大浪店',
          rooms: [
            {
              roomtypename: '双人标间',
              quantity: 1
            }
          ],
          checkintime: '2018-06-21',
          days: 1
        },
        needuploadphoto: true
      },
      invatation2: {
        meetingname: '2019年春季订货会反馈信息',
        cityname: '',
        customername: '',
        number: 1,
        list: '',
        arrivaltime: '2018-06-21',
        needbook: false,
        hotelinfo: {
          hotelname: '维也纳龙华大浪店',
          rooms: [
            {
              roomtypename: '双人标间',
              quantity: 1
            }
          ],
          checkintime: '2018-06-21',
          days: 1
        },
        needuploadphoto: true
      }
    }
  },
  methods: {
    submit() {
      if (this.invatation1.cityname.trim() === '') {
        this.msg('warning', '提醒', '请输入城市名称')
        return
      }
      if (this.invatation1.customername.trim() === '') {
        this.msg('warning', '提醒', '请输入客户姓名')
        return
      }
      if (this.invatation1.list.trim() === '') {
        this.msg('warning', '提醒', '请输入参加人员名单及联系方式')
        return
      }
      if (this.invatation2.list.trim() === '') {
        this.msg('warning', '提醒', '请输入参加人员名单')
        return
      }
      if (isNaN(this.invatation1.number) || isNaN(this.invatation2.number)) {
        this.msg('warning', '提醒', '请输入人数')
        return
      }
      if (!this.invatation1.arrivaltime) {
        this.msg('warning', '提醒', '请输入到达时间')
        return
      }
      if (!this.invatation2.arrivaltime) {
        this.msg('warning', '提醒', '请输入到达时间')
        return
      }
      if (this.invatation1.needbook) {
        if (!this.invatation1.hotelinfo.checkintime) {
          this.msg('warning', '提醒', '请输入住时间')
          return
        }
      } else {
        this.invatation1.hotelinfo.checkintime = new Date('1999-01-01')
      }
      if (this.invatation2.needbook) {
        if (!this.invatation2.hotelinfo.checkintime) {
          this.msg('warning', '提醒', '请输入住时间')
          return
        }
      } else {
        this.invatation2.hotelinfo.checkintime = new Date('1999-01-01')
      }
      this.invatation1.needuploadphoto = false
      this.invatation2.cityname = this.invatation1.cityname
      this.invatation2.customername = this.invatation1.customername
      this.$api.post(
        'invatation', [ this.invatation1 ],
        response => {
          if (response.status >= 200 && response.status < 300) {
            this.invatation1 = {
              meetingname: '2018年秋季商品培训反馈信息',
              cityname: '',
              customername: '',
              number: 1,
              list: '',
              arrivaltime: '2018-06-21',
              needbook: false,
              hotelinfo: {
                hotelname: '维也纳龙华大浪店',
                rooms: [
                  {
                    roomtypename: '双人标间',
                    quantity: 1
                  }
                ],
                checkintime: '2018-06-21',
                days: 1
              },
              needuploadphoto: true
            }

            this.$api.post(
              'invatation', [ this.invatation2 ],
              response => {
                if (response.status >= 200 && response.status < 300) {
                  this.invatation2 = {
                    meetingname: '2019年春季订货会反馈信息',
                    cityname: '',
                    customername: '',
                    number: 1,
                    list: '',
                    arrivaltime: '2018-06-21',
                    needbook: false,
                    hotelinfo: {
                      hotelname: '维也纳龙华大浪店',
                      rooms: [
                        {
                          roomtypename: '双人标间',
                          quantity: 1
                        }
                      ],
                      checkintime: '2018-06-21',
                      days: 1
                    },
                    needuploadphoto: true
                  }

                  this.msg('success', '提交成功', '感谢您提交反馈信息，请不要重复提交！</br>祝您有一个愉快的行程！')
                } else {
                  this.msg('error', '提交失败', '非常抱歉，因为网络或服务器原因反馈信息提交失败，请您稍候再试或者联系厂家方工作人员。')
                  console.log(response)
                }
              }
            )
          } else {
            this.msg('error', '提交失败', '非常抱歉，因为网络或服务器原因反馈信息提交失败，请您稍候再试或者联系厂家方工作人员。')
            console.log(response)
          }
        }
      )
    },
    msg (type, title, content) {
      switch (type) {
        case 'info':
          this.$Modal.info({
            title: title,
            content: content,
            transfer: false
          })
          break
        case 'success':
          this.$Modal.success({
            title: title,
            content: content,
            transfer: false
          })
          break
        case 'warning':
          this.$Modal.warning({
            title: title,
            content: content,
            transfer: false
          })
          break
        case 'error':
          this.$Modal.error({
            content: content
          })
          break
      }
    }
  }
}
</script>

<!-- Add 'scoped' attribute to limit CSS to this component only -->
<style scoped>
</style>
